    /*  
    championBuilder - Champions Online character planner.
    Copyright (C) 2009 Moritz Hartmeier
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You can find the GNU General Public License in the data folder.
    */


package ch.zarzu.champions.builder.gui;

import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.SpringLayout;

import ch.zarzu.champions.builder.logic.BuildUpdater;
import ch.zarzu.util.AppPath;

public class TabPane extends JLayeredPane implements MouseListener {
    LinkedList<JLayeredPane> tab_list;
    LinkedList<TabRider> riders;
    String app_path;
    SpringLayout layout;
    
    public TabPane(LinkedList<JLayeredPane> list) {
        tab_list = list;
        riders = new LinkedList<TabRider>();
        app_path = AppPath.get();
        //don't draw if there is no content
        if(list.size() > 0)
            draw();
    }
    
    private void draw() {
        layout = new SpringLayout();
        setLayout(layout);
        addMouseListener(this);
        
        JLabel edge = new JLabel(new ImageIcon(app_path + "images/tabs/tab_edge.png"));
        add(edge);
        
        ImageIcon icon_tmp = new ImageIcon(app_path + "images/tabs/tab_fade.png");
        ImageIcon icon = new ImageIcon(icon_tmp.getImage().getScaledInstance(2, icon_tmp.getIconHeight()*2, Image.SCALE_SMOOTH));
        JLabel fade = new JLabel(icon);
        add(fade);
        
        icon_tmp = new ImageIcon(app_path + "images/tabs/tab_line.png");
        icon = new ImageIcon(icon_tmp.getImage().getScaledInstance(2, 1200, Image.SCALE_SMOOTH));
        JLabel line = new JLabel(icon);
        add(line);
        
        layout.putConstraint(SpringLayout.WEST, edge, 0, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, edge, 17, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.SOUTH, line, 0, SpringLayout.NORTH, fade);
        layout.putConstraint(SpringLayout.WEST, line, 0, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, line, 0, SpringLayout.SOUTH, edge);
        layout.putConstraint(SpringLayout.SOUTH, fade, 0, SpringLayout.SOUTH, this);
        layout.putConstraint(SpringLayout.WEST, fade, 0, SpringLayout.WEST, this);
        
        for(JLayeredPane tab : tab_list) {
            riders.add(new TabRider(tab.getName()));
        }

        for(int i = 0; i < riders.size(); i++) {
            add(riders.get(i));
            riders.get(i).addMouseListener(this);
            if(i == 0)
                layout.putConstraint(SpringLayout.WEST, riders.get(0), 0, SpringLayout.EAST, edge);
            else
                layout.putConstraint(SpringLayout.WEST, riders.get(i), 0, SpringLayout.EAST, riders.get(i-1));
        }
        for(JLayeredPane tab : tab_list) {
            add(tab);
            layout.putConstraint(SpringLayout.EAST, tab, 0, SpringLayout.EAST, this);
            layout.putConstraint(SpringLayout.SOUTH, tab, 0, SpringLayout.SOUTH, this);
            layout.putConstraint(SpringLayout.WEST, tab, 0, SpringLayout.EAST, edge);
            layout.putConstraint(SpringLayout.NORTH, tab, 8, SpringLayout.SOUTH, riders.get(0));
            tab.setVisible(false);
        }
        tab_list.get(0).setVisible(true);
        riders.get(0).setActive();
    }
    
    public void switchTo(int i) {
        for(int j = 0; j < tab_list.size(); j++) {
            tab_list.get(j).setVisible(false);
            riders.get(j).setPassive();
        }
        tab_list.get(i).setVisible(true);
        riders.get(i).setActive();
        
        //if this is the build tab, update the power list
        if(tab_list.getFirst().getName().equals("powers")) {
            BuildUpdater.getInstance().enforceFilter();
        }
    }
    
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {}
    public void mouseClicked(MouseEvent e) {}

    public void mouseReleased(MouseEvent e) {
        for(int i = 0; i < riders.size(); i++) {
            if(e.getSource() == riders.get(i)) {
                switchTo(i);
                break;
            }
        }
    }
    
    public void updateLanguage() {
        for(TabRider r : riders) {
            r.updateLanguage();
        }
    }
}
